
function pay() {
    var wxChannel;
    var aliChannel;
    var channel;
    function startPay(goodsType,paytype ,number=1,goodsInfo) {
        var api_host = plus.storage.getItem('HOST_URL');
		
        // 从服务器请求支付订单
        var PAYSERVER = '';
        var ALIPAYSERVER = api_host + '/api/pay/alipay/index';
        var WXPAYSERVER = api_host + '/api/pay/wxpay/index';

        if (paytype == 'alipay') {
            PAYSERVER = ALIPAYSERVER;
            channel = aliChannel;
        } else if (paytype == 'wxpay') {
            PAYSERVER = WXPAYSERVER;
            channel = wxChannel;
        } else {
            plus.nativeUI.alert("不支持的支付类型哦！", null, "提示");
            return;
        }

        mui.ajax({
            url: PAYSERVER,
            dataType: 'json',
            data: {
                'type': goodsType,
                'goodsInfo': goodsInfo,
                'num': number
            },
			headers:{'User-Token':plus.storage.getItem('UserToken')},
            type: 'GET',
            async: true,
            timeout: 1000,
            success: function (data) {
                plus.payment.request(channel, data, function (result) {
                    plus.nativeUI.alert( '支付成功！',function () {
                        switch(goodsType){
                            case 'ebook':
                                var embed = plus.webview.currentWebview();
                                if(embed.id == 'ebookDetail.html'){
                                    embed.reload(true);
                                }else if(embed.id == 'tryRead.html'){
                                    mui.back();
                                }
                                break;
                            case 'group_question':
                                mui.back();
                                break;
                            case 'vip':
								getmember();
                                var embed = plus.webview.currentWebview();
                                if(embed.id == 'memberVip.html'){
                                	embed.reload(true);
                                }else if(embed.id == 'vipOrder.html'){
                                	mui.back();
                                }
                                break;
                            case 'course':
                                mui.openWindow({
                                    url:'courseDetail.html',
                                    extras:{'cid':goodsInfo.id},
                                });
                                break;
                            case 'shop':
                                // paymentSuccess();
                                mui.openWindow({
                                    url:'mallDetail.html',
                                    extras:{'product_id':goodsInfo.product_id},
                                    createNew:false,
                                    show:{
                                        autoshow:false,
                                    }
                                })
                                break;
                            default:
                                mui.toast('支付类型错误!', {
                                    duration: '5000'
                                });
                        }
                    }, '','确定');

                }, function (e) {
                    mui.toast('支付失败咯(T＿T)，您需要重新发起支付哦');
                });
            },
            error: function (xhr, type, errorThrown) {
                console.log(JSON.stringify(xhr))
            }
        });
    }
	//购买会员之后更新会员信息
    function getmember(){
		var api_host = plus.storage.getItem('HOST_URL');
		mui.ajax({
			url: api_host+'/api/linxuan/user/getmember',
			dataType: 'json',
			type: 'POST',
			headers:{'User-Token':plus.storage.getItem('UserToken')},
			timeout:3000,
			success: function (msg) {
				if (msg.code == 1) { 
					
				}
			},
			error: function (xhr, type, errorThrown) {
				mui.toast("会员信息更新失败，请重新登录");
			}
		})
	}
	
    /*
    * 参数说明
    * @goodsType string 支付的商品类型 例如 ebook
    * @paytype string 支付类型 alipay或者wxpay
    * @number int 购买数量
    * @goodsInfo object 其它商品参数 主要用于订单页面展示 包括商品ID名称等
    */

    this.start = function (goodsType,paytype,number,goodsInfo) {
        mui.plusReady(function () {
            plus.payment.getChannels(function (channels) {
                for (var i in channels) {
                    if (channels[i].id == "wxpay") {
                        wxChannel = channels[i];
                    } else {
                        aliChannel = channels[i];
                    }
                }
                startPay(goodsType,paytype,number,goodsInfo);
            }, function (e) {
                mui.toast("获取支付通道失败：" + e.message, {
                    duration: '2000',
                    type: 'div'
                });
            });
        });
    }
}